﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.SharePoint;

namespace ITSP.EventReceiverConfig
{
    /// <summary>
    /// This class is a helper class that has a number of functions to make dealing with Event Receivers a bit easier.
    /// </summary>
    public class EventReceiverHelper : IDisposable
    {
        
        #region Fields

        private SPWeb _web;

        #endregion

        #region Constructor

        public EventReceiverHelper()
        {
            
        }

        public EventReceiverHelper(SPWeb web)
        {
            _web = web;
        }

        #endregion

        #region Static Methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="typename"></param>
        /// <returns></returns>
        public static SPEventReceiverType GetSPEventReceiverType(string typename)
        {
            SPEventReceiverType receiverType = SPEventReceiverType.InvalidReceiver;
            try
            {
                switch (typename)
                {
                    case "ItemAdded":
                        receiverType = SPEventReceiverType.ItemAdded;
                        break;
                    case "ItemAdding":
                        receiverType = SPEventReceiverType.ItemAdding;
                        break;
                    case "ItemUpdated":
                        receiverType = SPEventReceiverType.ItemUpdated;
                        break;
                    case "ItemUpdating":
                        receiverType = SPEventReceiverType.ItemUpdating;
                        break;
                    case "ItemDeleted":
                        receiverType = SPEventReceiverType.ItemDeleted;
                        break;
                    case "ItemDeleting":
                        receiverType = SPEventReceiverType.ItemDeleting;
                        break;
                    default:
                        receiverType = SPEventReceiverType.InvalidReceiver;
                        break;

                }
            }
            catch (Exception ex)
            {

                throw;
            }
            return receiverType;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Get the content type from SPWeb object
        /// </summary>
        /// <param name="web"></param>
        /// <param name="sContentTypeName">Name of content type to retrieve.</param>
        public SPContentType GetContentType(SPWeb web, string sContentTypeName)
        {
            SPContentType lookupContentType = null;

            try
            {
                if (web.ContentTypes[sContentTypeName] != null)
                {
                    lookupContentType = web.ContentTypes[sContentTypeName];
                }
                else
                {
                    string message = String.Format("Content Type: {0} not found in web {1}", sContentTypeName, web.Url);
                    Trace.TraceInformation(message);
                    throw new SPException(message);
                }
            }
            catch (SPException exception)
            {
                Trace.TraceWarning("Exception GetContentType: {0} : {1}", sContentTypeName, exception.Message);
                throw;
            }
            return lookupContentType;
        }

        /// <summary>
        /// Get the content type from SPWeb object
        /// </summary>
        /// <param name="web"></param>
        /// <param name="listName">Name of content type to retrieve.</param>
        public SPList GetList(SPWeb web, string listName)
        {
            SPList lookupList = null;

            try
            {
                if (web.Lists[listName] != null)
                {
                    lookupList = web.Lists[listName];
                }
                else
                {
                    string message = String.Format("List: {0} not found in web {1}", listName, web.Url);
                    Trace.TraceInformation(message);
                    throw new SPException(message);
                }
            }
            catch (ArgumentException argex)
            {
                string message = String.Format("List: {0} not found in web {1} error: {2}", listName, web.Url, argex.GetType());
                Trace.TraceInformation(message);
                throw new SPException(message);
            }
            catch (SPException exception)
            {
                Trace.TraceWarning("Exception List: {0} : {1}", listName, exception.Message);
                throw;
            }

            return lookupList;
        }

        /// <summary>
        /// Function to add an event receiver to a content type
        /// </summary>
        /// <param name="contentType"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public bool AddEventReceiver(SPContentType contentType, EventReceiverConfiguration configuration)
        {
            bool bSuccess = false;
            try
            {
                SPEventReceiverDefinitionCollection eventReceivers = contentType.EventReceivers;
                if (DoesEventReceiverExist(eventReceivers, configuration))
                {
                    throw new ApplicationException("Event Receiver already exists");
                }

                SPEventReceiverDefinition newEventDefinition =  contentType.EventReceivers.Add();
                newEventDefinition.Assembly = configuration.Assembly;
                newEventDefinition.Class = configuration.Class;
                newEventDefinition.Name = configuration.Name;
                newEventDefinition.SequenceNumber = configuration.SequenceNo;
                newEventDefinition.Type = configuration.EventReceiverType;
                newEventDefinition.Update();
                contentType.Update(true,false);
                bSuccess = true;
            }
            catch (Exception)
            {
                

                throw;
            }
            return bSuccess;
        }

        /// <summary>
        /// Function to add an event receiver to a list
        /// </summary>
        /// <param name="list"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public bool AddEventReceiver(SPList list, EventReceiverConfiguration configuration)
        {
            bool bSuccess = false;
            try
            {
                SPEventReceiverDefinitionCollection eventReceivers = list.EventReceivers;
                if(DoesEventReceiverExist(eventReceivers, configuration))
                {
                    throw new ApplicationException("Event Receiver already exists");
                }

                SPEventReceiverDefinition newEventDefinition = list.EventReceivers.Add();
                newEventDefinition.Assembly = configuration.Assembly;
                newEventDefinition.Class = configuration.Class;
                newEventDefinition.Name = configuration.Name;
                newEventDefinition.SequenceNumber = configuration.SequenceNo;
                newEventDefinition.Type = configuration.EventReceiverType;
                newEventDefinition.Update();
                list.Update();
                bSuccess = true;
            }
            catch (Exception)
            {


                throw;
            }
            return bSuccess;
        }

        /// <summary>
        /// Does an event receiver exist in the collection 
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public bool DoesEventReceiverExist(SPEventReceiverDefinitionCollection collection, EventReceiverConfiguration configuration)
        {
            bool bExist = false;
            try
            {
                SPEventReceiverDefinition definition = GetEventReceiver(collection, configuration);
                if (definition != null)
                {
                    bExist = true;
                }
            }
            catch (Exception)
            {
                throw;
            }
            return bExist;
        }

        /// <summary>
        /// Get an event receiver from the collection.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        private SPEventReceiverDefinition GetEventReceiver(SPEventReceiverDefinitionCollection collection, EventReceiverConfiguration configuration)
        {
            SPEventReceiverDefinition foundDefinition = null;
            try
            {
                foreach (SPEventReceiverDefinition definition in collection)
                {
                    if (definition.Name.ToLower() == configuration.Name.ToLower() && definition.Assembly.ToLower() == configuration.Assembly.ToLower() && definition.Class.ToLower() == configuration.Class.ToLower())
                    {
                        foundDefinition = definition;
                        break;
                    }
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            
            return foundDefinition;
        }

        /// <summary>
        /// Function to remove an event receiver from a content type
        /// </summary>
        /// <param name="contentType"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public bool RemoveEventReceiver(SPContentType contentType, EventReceiverConfiguration configuration)
        {
            bool bSuccess = false;
            try
            {
                SPEventReceiverDefinitionCollection eventReceivers = contentType.EventReceivers;
                if (!DoesEventReceiverExist(eventReceivers, configuration))
                {
                    throw new ApplicationException("Event Receiver does not exist");
                }

                SPEventReceiverDefinition definition = GetEventReceiver(eventReceivers, configuration);
                definition.Delete();
                Console.WriteLine("Definition deleted");
                contentType.Update(true);
                Console.WriteLine("Content Type Updated");
                bSuccess = true;
            }
            catch (Exception)
            {
                throw;
            }
            return bSuccess;
        }

        /// <summary>
        /// Function to remove an event receiver from a list or document library
        /// </summary>
        /// <param name="contentType"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public bool RemoveEventReceiver(SPList list, EventReceiverConfiguration configuration)
        {
            bool bSuccess = false;
            try
            {
                SPEventReceiverDefinitionCollection eventReceivers = list.EventReceivers;
                
                if (!DoesEventReceiverExist(eventReceivers, configuration))
                {
                    throw new ApplicationException("Event Receiver does not exists");
                }

                SPEventReceiverDefinition definition = GetEventReceiver(eventReceivers, configuration);
                definition.Delete();
                list.Update();
                bSuccess = true;
            }
            catch (Exception)
            {
                throw;
            }
            return bSuccess;
        }

        /// <summary>
        /// This function will enumerate through the specified assembly and return
        /// the types that are derived from SPEventReceiverBase
        /// </summary>
        /// <param name="assemblyfullname"></param>
        public List<EventReceiverClassInfo> GetEventReceiverObjectsFromAssembly(string assemblyfullname)
        {
            List<EventReceiverClassInfo> listEventReceiverObjects = new List<EventReceiverClassInfo>();

            try
            {

                System.Reflection.Assembly eventReceiverAssembly = null;
                try
                {
                    eventReceiverAssembly = System.Reflection.Assembly.LoadWithPartialName(assemblyfullname);
                }
                catch (System.IO.FileNotFoundException fileex)
                {

                    //try to load with full assembly name
                    eventReceiverAssembly = System.Reflection.Assembly.Load(assemblyfullname);
                }

                Type[] assemblyTypes = eventReceiverAssembly.GetTypes();

                //get all the types are related to the SPEventReceiverBase
                try
                {
                    //get a list of the base SPEventReceiverClassInfo objects to start the process.
                    List<EventReceiverClassInfo> listBaseEventReceiverClassInfo=GetEventReceiverTypesFromSharePointAssembly();
                    
                    //call recursive function to get event receivers from all the
                    foreach (EventReceiverClassInfo baseClassInfo in listBaseEventReceiverClassInfo)
                    {
                        listEventReceiverObjects.AddRange(GetEventReceiverTypesFromBaseType(assemblyTypes, baseClassInfo.GetClassType));
                    }
                    
                   
                
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                
                

            }
            catch (System.IO.FileNotFoundException fileex)
            {
                throw;
            }
            catch (Exception ex)
            {

                throw;
            }

            return listEventReceiverObjects;
        }

        /// <summary>
        /// This function will enumerate through the specified assembly and return
        /// the types that are derived from SPEventReceiverBase
        /// </summary>
        /// <param name="assemblyfullname"></param>
        public List<EventReceiverClassInfo> GetEventReceiverTypesFromSharePointAssembly()
        {
            List<EventReceiverClassInfo> listBaseEventReceiverObjects = new List<EventReceiverClassInfo>();

            try
            {
                string baseSharePointAssemblyName = "Microsoft.SharePoint";
                System.Reflection.Assembly eventReceiverAssembly = null;
                eventReceiverAssembly = System.Reflection.Assembly.LoadWithPartialName(baseSharePointAssemblyName);
                Type[] assemblyTypes = eventReceiverAssembly.GetTypes();

                //get all the types are related to the SPEventReceiverBase
                try
                {
                    //call recursive function to get event receivers from SPEventReceiverBase
                    listBaseEventReceiverObjects = GetEventReceiverTypesFromBaseType(assemblyTypes, typeof(SPEventReceiverBase));

                }
                catch (Exception ex)
                {
                    throw ex;
                }



            }
            catch (System.IO.FileNotFoundException fileex)
            {
                throw new ApplicationException("Cannot find base Microsoft.SharePoint assembly on system"); ;
            }
            catch (Exception ex)
            {

                throw;
            }

            return listBaseEventReceiverObjects;
        }


        /// <summary>
        /// Recursive function that returns all Event Receiver Class Types from a base type.
        /// 
        /// </summary>
        /// <param name="assemblyTypes"></param>
        /// <param name="checkBaseType"></param>
        /// <returns></returns>
        public List<EventReceiverClassInfo> GetEventReceiverTypesFromBaseType(Type[] assemblyTypes, Type checkBaseType)
        {
            List<EventReceiverClassInfo> listEventReceiverObjects = new List<EventReceiverClassInfo>();

            try
            {

                //get all the types are related to the SPEventReceiverBase
                try
                {
                    listEventReceiverObjects = (from ertype in assemblyTypes.AsQueryable()
                                                where (ertype.IsClass && (ertype.BaseType == checkBaseType))
                                                select new EventReceiverClassInfo(ertype)).ToList();

                    EventReceiverClassInfo classInfo = null;
                    for (int enumCount = 0; enumCount < listEventReceiverObjects.Count; enumCount++)
                    {
                        classInfo = listEventReceiverObjects[enumCount];
                        listEventReceiverObjects.AddRange(GetEventReceiverTypesFromBaseType(assemblyTypes, classInfo.GetClassType));
                    }
                    
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            catch (Exception ex)
            {

                throw;
            }

            return listEventReceiverObjects;
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
           if(_web!=null)
           {
               _web.Dispose();
           }
        }

        #endregion
    }

    /// <summary>
    /// This class defines an object which the utility uses to represent an event receiver's configuration or definition.
    /// </summary>
    public class EventReceiverConfiguration
    {
        public string Assembly;
        public string Class;
        public string Name;
        public int SequenceNo;
        public string EventReceiverTypeName;
        public SPEventReceiverType EventReceiverType;
        public string EventReceiverTargetType;
        public string EventReceiverTargetName;

        /// <summary>
        /// Default Constructor
        /// </summary>
        public EventReceiverConfiguration()
        {
     
        }

        /// <summary>
        /// Constructor to create the Event Receiver Configuration object.
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="className"></param>
        /// <param name="receiverName"></param>
        /// <param name="sequenceNo"></param>
        /// <param name="eventReceiverTypeName"></param>
        public EventReceiverConfiguration(string assembly, string className, string receiverName, int sequenceNo, string eventReceiverTypeName, string targetType, string targetName )
        {
            Assembly = assembly;
            Class = className;
            Name = receiverName;
            SequenceNo = sequenceNo;
            EventReceiverTypeName = eventReceiverTypeName;
            EventReceiverType = EventReceiverHelper.GetSPEventReceiverType(EventReceiverTypeName);
            EventReceiverTargetType = targetType;
            EventReceiverTargetName = targetName;
        }

        
    }

        /// <summary>
        /// 
        /// </summary>
        public class EventReceiverClassInfo
        {
            #region Private

            private Type _eventReceiverClassType;
            private List<string> _eventReceiverTypes = new List<string>();

            #endregion
            
            #region Fields

            /// <summary>
            /// 
            /// </summary>
            public string Assembly
            {
                get
                {
                    return _eventReceiverClassType.Assembly.ToString();
                }
            }

            /// <summary>
            /// 
            /// </summary>
            public string ClassName
            {
                get
                {
                    return _eventReceiverClassType.Name;
                }
            }

            /// <summary>
            /// 
            /// </summary>
            public string FullName
            {
                get
                {
                    return _eventReceiverClassType.FullName;
                }
            }

            /// <summary>
            /// 
            /// </summary>
            public List<string> GetEventReceiverTypes
            {
                get 
                {
                    return _eventReceiverTypes;
                }
            }

            public Type GetClassType
            {
                get
                {
                    return _eventReceiverClassType;
                }
            }

            #region Event Receiver Has Item Events

            /// <summary>
            /// Does the event receiver class have a function to process item added event
            /// </summary>
            public bool HasItemAddedEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemAdded);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process item adding event
            /// </summary>
            public bool HasItemAddingEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemAdding);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process item updating event
            /// </summary>
            public bool HasItemUpdatingEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemUpdating);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process item updated event
            /// </summary>
            public bool HasItemUpdatedEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemUpdated);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process item deleting event
            /// </summary>
            public bool HasItemDeletingEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemDeleting);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process item deleted event
            /// </summary>
            public bool HasItemDeletedEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemDeleted);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process item attachment added event
            /// </summary>
            public bool HasItemAttachmentAddedEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemAttachmentAdded);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process item attachment adding event
            /// </summary>
            public bool HasItemAttachmentAddingEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemAttachmentAdding);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process item attachment deleted event
            /// </summary>
            public bool HasItemAttachmentDeletedEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemAttachmentDeleted);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process item attachment deleting event
            /// </summary>
            public bool HasItemAttachmentDeletingEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemAttachmentDeleting);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process item checked in event
            /// </summary>
            public bool HasItemCheckedInEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemCheckedIn);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process item checked out event
            /// </summary>
            public bool HasItemCheckedOutEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemCheckedOut);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process item checking in event
            /// </summary>
            public bool HasItemCheckingInEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemCheckingIn);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process item checking out event
            /// </summary>
            public bool HasItemCheckingOutEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemCheckingOut);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process item unchecked out event
            /// </summary>
            public bool HasItemUncheckedOutEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemUncheckedOut);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process item unchecking out event
            /// </summary>
            public bool HasItemUncheckingOutEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemUncheckingOut);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process file converting event 
            /// </summary>
            public bool HasItemFileConvertedEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemFileConverted);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process file moved event 
            /// </summary>
            public bool HasItemFileMovedEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemFileMoved);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process file moving event 
            /// </summary>
            public bool HasItemFileMovingEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ItemFileMoving);
                }
            }

            #endregion

            #region Event Receiver Has Web Events

            /// <summary>
            /// Does the event receiver class have a function to process web deleted event 
            /// </summary>
            public bool HasWebDeletedEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.WebDeleted);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process web deleting event 
            /// </summary>
            public bool HasWebDeletingEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.WebDeleting);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process web moved event 
            /// </summary>
            public bool HasWebMovedEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.WebMoved);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process web moving event 
            /// </summary>
            public bool HasWebMovingEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.WebMoving);
                }
            }


            #endregion

            #region Event Receiver Has Site Events

            /// <summary>
            /// Does the event receiver class have a function to process site collection deleted event 
            /// </summary>
            public bool HasSiteDeletedEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.SiteDeleted);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process site collection deleting event 
            /// </summary>
            public bool HasSiteDeletingEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.SiteDeleting);
                }
            }

            #endregion

            #region Event Receiver Has Field Events

            /// <summary>
            /// Does the event receiver class have a function to process field added event 
            /// </summary>
            public bool HasFieldAddedEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.FieldAdded);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process field adding event 
            /// </summary>
            public bool HasFieldAddingEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.FieldAdding);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process field deleted event 
            /// </summary>
            public bool HasFieldDeletedEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.FieldDeleted);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process field deleting event 
            /// </summary>
            public bool HasFieldDeletingEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.FieldDeleting);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process field updated event 
            /// </summary>
            public bool HasFieldUpdatedEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.FieldUpdated);
                }
            }

            /// <summary>
            /// Does the event receiver class have a function to process field updating event 
            /// </summary>
            public bool HasFieldUpdatingEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.FieldUpdating);
                }
            }

            #endregion

            #region Has Context Event Receiver

            /// <summary>
            /// Does the event receiver class have a function to process a context event
            /// </summary>
            public bool HasContextEventEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.ContextEvent);
                }
            }


            #endregion

            #region Has Email Received Event Receiver

            /// <summary>
            /// Does the event receiver class have a function to process an email received event
            /// </summary>
            public bool HasEmailReceivedEvent
            {
                get
                {
                    return ContainsEventReceiverType(SPEventReceiverType.EmailReceived);
                }
            }


            #endregion

            #endregion

            #region Constructors
            public EventReceiverClassInfo()
            {
            }

            public EventReceiverClassInfo(Type eventReceiverType)
            {
                _eventReceiverClassType = eventReceiverType;
                Initialise();
            }
            #endregion

            #region Methods

            /// <summary>
            /// Sets up the event receiver class information object
            /// </summary>
            protected void Initialise()
            {
                try
                {
                    //resolve which event receiver types exist
                    System.Reflection.MethodInfo[] methodInfo = _eventReceiverClassType.GetMethods();

                    System.Reflection.MethodInfo[] eventReceiverMethodInfo = null;
                    foreach (System.Reflection.MethodInfo checkMethod in methodInfo)
                    {
                        Type eventReceiverTypes = typeof(SPEventReceiverType);
                        if(eventReceiverTypes.IsEnum)
                        {
                            eventReceiverMethodInfo = eventReceiverTypes.GetMethods();
                            
                        }

                        foreach(System.Reflection.MemberInfo info in eventReceiverMethodInfo)
                        {
                            if (checkMethod.Name == info.Name.ToString())
                            {
                                _eventReceiverTypes.Add(info.Name);
                            }
                        }
                        
                    }

                    //resolve which event receiver operations are supported

                }
                catch (Exception ex)
                {
                }
            }

            /// <summary>
            /// Does the EventReceiverClass have a function which implements an <see cref="SPEventReceiverType"/> ?
            /// </summary>
            /// <param name="eventReceiverType"></param>
            /// <returns></returns>
            protected bool ContainsEventReceiverType(SPEventReceiverType eventReceiverType)
            {
                bool bContains = false;

                try
                {
                    if(_eventReceiverClassType!=null)
                    {
                        System.Reflection.MemberInfo[] methods = _eventReceiverClassType.GetMembers();
                        System.Reflection.MemberInfo method = methods.SingleOrDefault(m=>m.Name==eventReceiverType.ToString());
                        if(method!=null)
                        {
                            bContains=true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw;
                }

                return bContains;
            }

            /// <summary>
            /// Creates a string which can be used to display a formatted list of information.
            /// </summary>
            /// <returns></returns>
            public string DisplayInfo()
            {
                string outputDisplayInfo = String.Empty;
                try
                {
                    StringBuilder builderEvents = new StringBuilder();
                    StringBuilder outputBuilder = new StringBuilder();
                    bool onlyShowEnabledEvents = true;

                    //assembly
                    outputBuilder.AppendLine(String.Format("Assembly: {0}", Assembly));

                    //class name
                    outputBuilder.AppendLine(String.Format("Class: {0}", ClassName));

                    //inherits
                    outputBuilder.AppendLine(String.Format("Inherits: {0}", _eventReceiverClassType.BaseType.Name));

                    //has item updated/updating event receiver etc
                    outputBuilder.AppendLine("Event Receiver Types");
                    if(!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemAddedEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item Added: {0}", HasItemAddedEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemAddingEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item Adding: {0}", HasItemAddingEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemUpdatedEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item Updated: {0}", HasItemUpdatedEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemUpdatingEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item Updating: {0}", HasItemUpdatingEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemDeletedEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item Deleted: {0}", HasItemDeletedEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemDeletingEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item Deleting: {0}", HasItemDeletingEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemAttachmentAddedEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item Attachment Added: {0}", HasItemAttachmentAddedEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemAttachmentAddingEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item Attachment Adding: {0}", HasItemAttachmentAddingEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemAttachmentDeletedEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item Attachment Deleted: {0}", HasItemAttachmentDeletedEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemAttachmentDeletingEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item Attachment Deleting: {0}", HasItemAttachmentDeletingEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemCheckedInEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item Checked In: {0}", HasItemCheckedInEvent.ToString()));
                    }
                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemCheckingInEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item Checking In: {0}", HasItemCheckingInEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemCheckedOutEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item Checked Out: {0}", HasItemCheckedOutEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemCheckingOutEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item Checking Out: {0}", HasItemCheckingOutEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemFileMovedEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item File Moved: {0}", HasItemFileMovedEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemFileMovingEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item File Moving: {0}", HasItemFileMovingEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemUncheckedOutEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item Unchecked Out: {0}", HasItemUncheckedOutEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemUncheckingOutEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item Unchecking out: {0}", HasItemUncheckingOutEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasItemFileConvertedEvent))
                    {
                        builderEvents.AppendLine(String.Format("Item File Converted: {0}", HasItemFileConvertedEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasFieldAddedEvent))
                    {
                        builderEvents.AppendLine(String.Format("Field Added: {0}", HasFieldAddedEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasFieldAddingEvent))
                    {
                        builderEvents.AppendLine(String.Format("Field Adding: {0}", HasFieldAddingEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasFieldUpdatedEvent))
                    {
                        builderEvents.AppendLine(String.Format("Field Updated: {0}", HasFieldUpdatedEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasFieldUpdatingEvent))
                    {
                        builderEvents.AppendLine(String.Format("Field Updating: {0}", HasFieldUpdatingEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasFieldDeletedEvent))
                    {
                        builderEvents.AppendLine(String.Format("Field Deleted: {0}", HasFieldDeletedEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasFieldDeletingEvent))
                    {
                        builderEvents.AppendLine(String.Format("Field Deleting: {0}", HasFieldDeletingEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasWebDeletedEvent))
                    {
                        builderEvents.AppendLine(String.Format("Web Deleted: {0}", HasWebDeletedEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasWebDeletingEvent))
                    {
                        builderEvents.AppendLine(String.Format("Web Deleting: {0}", HasWebDeletingEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasWebMovedEvent))
                    {
                        builderEvents.AppendLine(String.Format("Web Moved: {0}", HasWebMovedEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasWebMovingEvent))
                    {
                        builderEvents.AppendLine(String.Format("Web Moving: {0}", HasWebMovingEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasSiteDeletedEvent))
                    {
                        builderEvents.AppendLine(String.Format("Site Deleted: {0}", HasSiteDeletedEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasSiteDeletingEvent))
                    {
                        builderEvents.AppendLine(String.Format("Site Deleting: {0}", HasSiteDeletingEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasContextEventEvent))
                    {
                        builderEvents.AppendLine(String.Format("Context Event: {0}", HasContextEventEvent.ToString()));
                    }

                    if (!onlyShowEnabledEvents || (onlyShowEnabledEvents && HasEmailReceivedEvent))
                    {
                        builderEvents.AppendLine(String.Format("Email Received: {0}", HasEmailReceivedEvent.ToString()));
                    }

                    if(String.IsNullOrEmpty(builderEvents.ToString()))
                    {
                        builderEvents.AppendLine("No Event Receiver Events");
                    }
                    outputDisplayInfo = outputBuilder.ToString() + builderEvents.ToString();
                }
                catch (Exception ex)
                {
                    throw;
                }

                return outputDisplayInfo;
            }

            #endregion
        }

}
